
const db = wx.cloud.database();





Page({
  data: {
    questions: [], //题库
  },


 



  onLoad: function(options) {
    var that = this
    db.collection('error').get({ //获取数据库中的题库，保存到本地
      success: res => {
        console.log(res)
        that.setData({
          questions: res.data
        })
        for (let i = 0; i < that.data.questions.length;i++){ 
            that.data.questions[i]['options'] = []
            that.data.questions[i]['options'].push({
              checked:false,
              name:that.data.questions[i].A,
              value:'A'
            })
            that.data.questions[i]['options'].push({
              checked:false,
              name:that.data.questions[i].B,
              value:'B'
            })
            that.data.questions[i]['options'].push({
              checked:false,
              name:that.data.questions[i].C,
              value:'C'
            })
            that.data.questions[i]['options'].push({
              checked:false,
              name:that.data.questions[i].D,
              value:'D'
            })

        }
        that.setData({
          questions:that.data.questions
        })
        console.log(that.data.questions);
        var oldArr = [];
        var cardArr = [];
        for (let i = 0; i < this.data.questions.length; i++) {
          oldArr.push(i); //创建一个新的数组用作索引
          cardArr.push(false); //创建答题卡数组
        }
        this.setData({
          cardArr: cardArr,
        }) //将数组保存作为答题卡渲染
        console.log(this.data.cardArr)
        var newArr = [];
        while (oldArr.length) {
          var index = parseInt(Math.random() * oldArr.length);
          newArr = newArr.concat(oldArr.splice(index, 1)) //随机替换旧数组索引，生成新的随机数组
        }
        console.log(newArr);
        this.setData({
          newArr: newArr,
        })
      }
    })
  },


})
